from rest_framework.decorators import action
from rest_framework.mixins import CreateModelMixin, ListModelMixin, RetrieveModelMixin, DestroyModelMixin
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from social.feed.models import Post, Comment, PostLike
from social.feed.serializers import PostSerializer, PostCommentSerializer, PostLikeUserSerializer, PostLikeSerializer
from social.user.models import UserLike
from social.utils.views import BaseGenericViewSet, UpdateModelMixin


class PostViewSet(BaseGenericViewSet, CreateModelMixin, UpdateModelMixin, ListModelMixin, RetrieveModelMixin,
                  DestroyModelMixin):
    queryset = Post.objects.select_related('user').all()
    serializer_class = PostSerializer
    permission_classes = (IsAuthenticated,)

    def retrieve(self, request, *args, **kwargs):
        return super(PostViewSet, self).retrieve(request, *args, **kwargs)

    def create(self, request, *args, **kwargs):
        ser = PostSerializer(data=request.data, context={'request': request})
        if ser.is_valid(raise_exception=True):
            ser.save(user=request.user)
        return Response(data=ser.data)

    @action(methods=['POST'], detail=True)
    def like(self, request, *args, **kwargs):
        post = self.get_object()
        post.like(request.user)
        return Response()

    @action(methods=['POST'], detail=True)
    def dis_like(self, request, *args, **kwargs):
        post = self.get_object()
        post.dis_like(request.user)
        return Response()

    @action(methods=['POST'], detail=True)
    def comment(self, request, *args, **kwargs, ):
        ser = PostCommentSerializer(data=request.data, context={'request': request, 'post': self.get_object()})
        if ser.is_valid(raise_exception=True):
            ser.save()
        return Response(data=ser.data)

    @action(methods=['GET'], detail=True, serializer_class=PostCommentSerializer)
    def comment_list(self, request, *args, **kwargs):
        self.queryset = Comment.objects.select_related('user', 'child', 'reply_user').filter(post=self.get_object(),
                                                                                             parent__isnull=True,
                                                                                             is_active=True)
        return super(PostViewSet, self).list(request, *args, **kwargs)

    @action(methods=['GET'], detail=True, serializer_class=PostLikeUserSerializer)
    def post_like_list(self, request, *args, **kwargs):
        self.queryset = PostLike.objects.select_related('user').filter(post=self.get_object())
        return super(PostViewSet, self).list(request, *args, **kwargs)

    @action(methods=['GET'], detail=False, serializer_class=PostSerializer)
    def followers_post_list(self, request, *args, **kwargs):
        like_ids = UserLike.objects.values_list('to_user_id', flat=True).filter(from_user=request.user)
        self.queryset = Post.objects.select_related('user').filter(user__in=like_ids, is_active=True)
        return super(PostViewSet, self).list(request, *args, **kwargs)
